IF exists(SELECT * FROM dbo.sysobjects WHERE name='geefKlassenSamenMet' AND xtype='P') 
BEGIN
	DROP PROCEDURE geefKlassenSamenMet
END
GO

CREATE procedure geefKlassenSamenMet
(@klas varchar(5),
 @vak char(2))
as
set nocount on
declare @tabel table(klas varchar(5))
declare @deKlas varchar(5)

select @deKlas = samenMet from leraars..ambtsverdeling 
where vakcode = @vak and klas=@klas

if (@deKlas is Null) or len(@deKlas) < 1 
  begin
       insert into @tabel(klas)
	values(@klas)

	insert into @tabel(klas)
	select klas from leraars..ambtsverdeling
	where vakcode=@vak and samenMet = @klas
  end
else
  begin
	insert into @tabel(klas)
	values(@deKlas)

	insert into @tabel(klas)
	select klas from leraars..ambtsverdeling
	where vakcode=@vak and samenMet = @deKlas
  end
select distinct * from @tabel